package com.lc.user.express.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lc.user.express.R;
import com.lc.user.express.app.AppContext;
import com.lc.user.express.model.AddrModel;

import java.util.List;


/**
 * Created by Administrator on 2016/3/3.
 */
public class AddrAdapter extends BaseListAdapter<AddrModel> {
    private boolean isShow = true;//是否显示箭头
    public AddrAdapter(Context context, List<AddrModel> data, boolean isShow) {
        super(context, data);
        this.isShow = isShow;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (position == 0) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_addr_list1, null);
        } else if (position == data.size()-1) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_addr_list1, null);
        } else {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_addr_list2, null);
        }
        AppContext.ScaleScreenHelper.loadView((ViewGroup) convertView);
        ImageView iv_pic = (ImageView) convertView.findViewById(R.id.iv_pic);
        TextView tv_addr = (TextView) convertView.findViewById(R.id.tv_addr);
        TextView tv_contact = (TextView) convertView.findViewById(R.id.tv_contact);
        TextView tv_line = (TextView) convertView.findViewById(R.id.tv_line);
        ImageView iv_arrow = (ImageView)convertView.findViewById(R.id.iv_arrow);
        if (position == 0) {
            iv_pic.setBackgroundResource(R.mipmap.start);
            tv_addr.setText(TextUtils.isEmpty(data.get(0).getAddr()) ? "请输入始发地" : data.get(0).getAddr());
        } else if (position == data.size()-1) {
            iv_pic.setBackgroundResource(R.mipmap.end);
            tv_addr.setText(TextUtils.isEmpty(data.get(data.size() - 1).getAddr()) ? "请输入目的地" : data.get(data.size() - 1).getAddr());
        } else {
            iv_pic.setBackgroundResource(R.mipmap.mid);
            tv_addr.setText(TextUtils.isEmpty(data.get(position).getAddr()) ? "请输入途径地" : data.get(position).getAddr());
        }

        iv_arrow.setVisibility(isShow?View.VISIBLE:View.GONE);
        if(TextUtils.isEmpty(data.get(position).getName()) && TextUtils.isEmpty(data.get(position).getMobile())){
            tv_contact.setVisibility(View.GONE);
        }else {
            tv_contact.setVisibility(View.VISIBLE);
            if(TextUtils.isEmpty(data.get(position).getMobile())){
                tv_contact.setText(data.get(position).getName());
            }else {
                tv_contact.setText(data.get(position).getMobile()+"  "+data.get(position).getName());
            }

        }

        if(position >= data.size()-2 || position == 0){
            tv_line.setVisibility(View.GONE);
        }else {
            tv_line.setVisibility(View.VISIBLE);
        }
        return convertView;
    }
}
